package contegor.scire.core.modifications;

import contegor.scire.core.Container;
import contegor.scire.core.Id;
import contegor.scire.core.IdFragment;
import contegor.scire.core.MissingElementException;
import contegor.scire.core.Modifiable;
import contegor.scire.core.Primitive;
import contegor.scire.core.TransactionException;
import contegor.scire.core.Version;
import contegor.scire.core.WrongStateException;

/**
 * {@link Modification} used to modify a {@link Primitive}'s value.
 *
 * @author  Héctor Cirbián Pousa
 */
@Version("$Id$")
public class SetPrimitiveValue extends Modification<Primitive> {
  private static final long serialVersionUID = 2886787493989581794L;

  private Object value;
  private IdFragment primitiveIdFragmet;

  public SetPrimitiveValue(Id primitiveId, Object value) {
    super(primitiveId.getParentId());
    this.primitiveIdFragmet = primitiveId.getLastIdFragment();
    this.value = value;
  }

  public void process(Container modifiable) throws TransactionException, MissingElementException, WrongStateException {
    super.isValidForThisContainer(modifiable);
    Primitive primitive = ((Modifiable)modifiable).setPrimitiveValue(relatedTime, this.taskId.getTransactionId(), primitiveIdFragmet, value);
    returnPoint.manage(this.taskId, primitive);
  }
}
